package com.ruoyi.loginAndregist.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;

//短信工具类

@Component
public class SmsUtil {

  /*  private static String accessKeyId; //访问密钥ID
    private static String accessSecret;//密钥值
    private static String templeteCode;//短信模板

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    public void setAccessSecret(String accessSecret) {
        this.accessSecret = accessSecret;
    }

    public String getTempleteCode() {
        return templeteCode;
    }

    public void setTempleteCode(String templeteCode) {
        this.templeteCode = templeteCode;
    }*/

    public void  sendSms(String phoneNumber, String code) throws IOException {



    /*    //配置文件类 ,读取配置文件
      //  DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9ZRUFwAw7p7q8FRRxe", "U9K3WEEZ3h1UY9mxr3cUJmfgu7tWpx");
        IAcsClient client = new DefaultAcsClient(profile);

        System.out.println(profile);

        //配置请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");   //查询参数  , 服务器所在地

        //设置 手机号
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        //设置标签名称
        request.putQueryParameter("SignName","智慧云街");
        //模板 code
       // request.putQueryParameter("TemplateCode", templeteCode);
        request.putQueryParameter("TemplateCode", "SMS_181852874");
        //模板是一个json样式
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);    //通过客户端拿到 阿里服务器 响应的 短信内容
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;*/


        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9ZRUFwAw7p7q8FRRxe", "U9K3WEEZ3h1UY9mxr3cUJmfgu7tWpx");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",  phoneNumber);
        request.putQueryParameter("SignName", "智慧云街");
        request.putQueryParameter("TemplateCode", "SMS_181852874");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");



        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取4位随机数
     * @return
     */
    public static String getFourRandom(){
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if(randLength<4){
            for(int i=1; i<=4-randLength; i++)
                fourRandom = "0" + fourRandom ;
        }
        return fourRandom;
    }

    public static void main(String[] args) {
        String fourRandom = getFourRandom();
        System.out.print(fourRandom);
    }
}
